package io.gitee.zhangbinhub.admin.workflow.controller.api

import cn.dev33.satoken.annotation.SaCheckPermission
import io.gitee.zhangbinhub.acp.boot.exceptions.WebException
import io.gitee.zhangbinhub.acp.boot.log.LogAdapter
import io.gitee.zhangbinhub.admin.common.api.WorkFlowApi
import io.gitee.zhangbinhub.admin.common.constant.ModuleFuncCode
import io.gitee.zhangbinhub.admin.common.constant.RoleCode
import io.gitee.zhangbinhub.admin.common.vo.CustomerQueryPageVo
import io.gitee.zhangbinhub.admin.common.vo.InfoVo
import io.gitee.zhangbinhub.admin.resource.server.base.BaseResourceServerController
import io.gitee.zhangbinhub.admin.resource.server.vo.ProcessDefinitionVo
import io.gitee.zhangbinhub.admin.workflow.po.ProcessDefinitionPo
import io.gitee.zhangbinhub.admin.workflow.po.ProcessDefinitionQueryPo
import io.gitee.zhangbinhub.admin.workflow.service.WorkFlowDefinitionService
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.noear.solon.annotation.Body
import org.noear.solon.annotation.Controller
import org.noear.solon.annotation.Mapping
import org.noear.solon.annotation.Path
import org.noear.solon.core.handle.Context
import org.noear.solon.core.handle.MethodType
import org.noear.solon.core.util.MimeType
import org.noear.solon.validation.annotation.NotEmpty
import org.noear.solon.validation.annotation.NotNull
import org.noear.solon.validation.annotation.Valid
import org.noear.solon.validation.annotation.Validated

@Valid
@Controller
@Api("工作流部署")
open class WorkFlowDefinitionController(
    logAdapter: LogAdapter,
    private val workFlowDefinitionService: WorkFlowDefinitionService
) : BaseResourceServerController(logAdapter) {

    @ApiOperation("新建工作流信息")
    @SaCheckPermission(ModuleFuncCode.flowDefinition, orRole = [RoleCode.SUPER])
    @Mapping(
        method = [MethodType.PUT],
        value = WorkFlowApi.basePath + WorkFlowApi.definition,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun create(@Body @Validated processDefinitionPo: ProcessDefinitionPo): InfoVo =
        workFlowDefinitionService.create(processDefinitionPo).let {
            InfoVo(message = "创建成功")
        }

    @ApiOperation("删除指定的工作流信息")
    @SaCheckPermission(ModuleFuncCode.flowDefinition, orRole = [RoleCode.SUPER])
    @Mapping(
        method = [MethodType.DELETE],
        value = WorkFlowApi.basePath + WorkFlowApi.definition,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    fun delete(
        @ApiParam(name = "id列表", required = true)
        @NotEmpty(message = "id不能为空")
        @NotNull(message = "id不能为空")
        @Body
        idList: MutableList<Long>
    ): InfoVo = workFlowDefinitionService.delete(idList).let {
        InfoVo(message = "删除成功")
    }

    @ApiOperation("更新指定的工作流信息")
    @SaCheckPermission(ModuleFuncCode.flowDefinition, orRole = [RoleCode.SUPER])
    @Mapping(
        method = [MethodType.PATCH],
        value = WorkFlowApi.basePath + WorkFlowApi.definition,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun update(@Body @Validated processDefinitionPo: ProcessDefinitionPo): InfoVo {
        if (processDefinitionPo.id == null) {
            throw WebException("ID不能为空")
        }
        return workFlowDefinitionService.update(processDefinitionPo).let {
            InfoVo(message = "更新成功")
        }
    }

    @ApiOperation("查询工作流信息列表")
    @SaCheckPermission(ModuleFuncCode.flowDefinition, orRole = [RoleCode.SUPER])
    @Mapping(
        method = [MethodType.POST],
        value = WorkFlowApi.basePath + WorkFlowApi.definition,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun query(@Body @Validated processDefinitionQueryPo: ProcessDefinitionQueryPo): CustomerQueryPageVo<ProcessDefinitionVo> =
        workFlowDefinitionService.query(processDefinitionQueryPo)

    @ApiOperation("发布工作流")
    @SaCheckPermission(ModuleFuncCode.flowDefinition, orRole = [RoleCode.SUPER])
    @Mapping(
        method = [MethodType.POST],
        value = WorkFlowApi.basePath + WorkFlowApi.definitionPublish + "/{id}",
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun publish(@Path(name = "id") id: Long): InfoVo =
        workFlowDefinitionService.publish(id).let {
            InfoVo(message = "发布成功")
        }

    @ApiOperation("取消发布工作流")
    @SaCheckPermission(ModuleFuncCode.flowDefinition, orRole = [RoleCode.SUPER])
    @Mapping(
        method = [MethodType.DELETE],
        value = WorkFlowApi.basePath + WorkFlowApi.definitionPublish + "/{id}",
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun unPublish(@Path(name = "id") id: Long): InfoVo =
        workFlowDefinitionService.unPublish(id).let {
            InfoVo(message = "取消发布成功")
        }

    @ApiOperation("激活工作流")
    @SaCheckPermission(ModuleFuncCode.flowDefinition, orRole = [RoleCode.SUPER])
    @Mapping(
        method = [MethodType.POST],
        value = WorkFlowApi.basePath + WorkFlowApi.definitionActive + "/{id}",
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun active(@Path(name = "id") id: Long): InfoVo = workFlowDefinitionService.active(id).let {
        InfoVo(message = "激活成功")
    }

    @ApiOperation("挂起工作流")
    @SaCheckPermission(ModuleFuncCode.flowDefinition, orRole = [RoleCode.SUPER])
    @Mapping(
        method = [MethodType.DELETE],
        value = WorkFlowApi.basePath + WorkFlowApi.definitionActive + "/{id}",
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun unActive(@Path(name = "id") id: Long): InfoVo =
        workFlowDefinitionService.unActive(id).let {
            InfoVo(message = "挂起成功")
        }

    @ApiOperation("流程定义文件导入")
    @Mapping(
        method = [MethodType.POST],
        value = WorkFlowApi.basePath + WorkFlowApi.definitionImport,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun import(@Body processDefinitionPo: ProcessDefinitionPo): InfoVo =
        workFlowDefinitionService.import(processDefinitionPo).let {
            InfoVo(message = "导入成功")
        }

    @ApiOperation("流程定义文件导出")
    @Mapping(
        method = [MethodType.GET],
        value = WorkFlowApi.basePath + WorkFlowApi.definitionExport + "/{id}",
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun export(context: Context, @Path(name = "id") id: Long) {
        workFlowDefinitionService.export(context, id)
    }
}
